package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.service.base.SubAreaService;
import cn.itcast.bos.utils.PinYin4jUtils;
import cn.itcast.bos.web.action.common.BaseAction;
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class SubAreaAction extends BaseAction<SubArea>{
	private static final long serialVersionUID = 1L;

	@Autowired
	private SubAreaService subAreaService;
	
	private File file;
	private String fileFileName;
	private String fileContentType;
	
	@Action(value="subArea_batchImport")
	public String batchImport() throws FileNotFoundException, IOException {
		List<SubArea> list = new ArrayList<SubArea>();
		
		Workbook workbook = null;
		Sheet sheet = null;
		if(fileFileName.endsWith(".xls")) {
			workbook = new HSSFWorkbook(new FileInputStream(file));
		} else if(fileFileName.endsWith(".xlsx")) {
			workbook = new XSSFWorkbook(new FileInputStream(file));
		}
		
		if(workbook !=null ) {
			sheet = workbook.getSheetAt(0);
		}
		
		if(sheet != null) {
			for (Row row : sheet) {
				// 一行数据 对应 一个区域对象
				if (row.getRowNum() == 0) {
					// 第一行 跳过
					continue;
				}
				// 跳过空行
				if (row.getCell(0) == null
						|| StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
					continue;
				}
				
				SubArea subarea = new SubArea();
				subarea.setKeyWords(row.getCell(5).getStringCellValue());
				subarea.setAssistKeyWords(row.getCell(9).getStringCellValue());
				subarea.setStartNum(row.getCell(6).getStringCellValue());
				subarea.setEndNum(row.getCell(7).getStringCellValue());
				//subarea.setSingle(row.getCell(8));
				
				//subarea.setId(row.getCell(0).getStringCellValue());
				// 基于pinyin4j生成城市编码和简码
			/*	String province = area.getProvince();
				String city = area.getCity();
				String district = area.getDistrict();
				province = province.substring(0, province.length() - 1);
				city = city.substring(0, city.length() - 1);
				district = district.substring(0, district.length() - 1);
				// 简码
				String[] headArray = PinYin4jUtils.getHeadByString(province + city
						+ district);
				StringBuffer buffer = new StringBuffer();
				for (String headStr : headArray) {
					buffer.append(headStr);
				}
				String shortcode = buffer.toString();
				area.setShortcode(shortcode);
				// 城市编码
				String citycode = PinYin4jUtils.hanziToPinyin(city, "");
				area.setCitycode(citycode);
	
				areas.add(area);*/
			}
			// 调用业务层
			//subAreaService.saveBatch(areas);
		}
		return NONE;
	}
}
